package org.huntaway.kindergarten.manager;

import org.hibernate.Criteria;
import org.huntaway.hibernate.SessionManager;
import org.huntaway.kernel.NutException;
import org.huntaway.kernel.TimesTools;
import org.huntaway.kernel.imp.TimesToolsImp;
import org.huntaway.kindergarten.common.SingletonManager;
import org.huntaway.kindergarten.po.base.BasePo;
import org.huntaway.kindergarten.util.GlobalMessage;
import org.huntaway.kindergarten.util.PageResult;
import org.huntaway.kindergarten.util.Singleton;

public class BaseMng<T extends BasePo> {
	protected SessionManager dateManage = Singleton.dateManage;
	protected SingletonManager sm = SingletonManager.getInstance();
	protected TimesTools timeTools=new TimesToolsImp();
	@SuppressWarnings("unchecked")
	public T getEntityById(@SuppressWarnings("rawtypes") Class clazz, Integer id) {
		return (T) dateManage.load(clazz, id);
	}

	@SuppressWarnings("unchecked")
	public T load(Class<? extends Object> clazz, Integer keyId) {
		return (T) dateManage.get(clazz, keyId);
	}

	protected void deleteById(@SuppressWarnings("rawtypes") Class clazz,
			Integer id) {
		dateManage.delete(clazz, id);
	}

	public void save(T obj) {
		dateManage.save(obj);
	}

	public void delete(T item) throws NutException {
		dateManage.delete(item);
	}

	public void update(T item) {
		dateManage.update(item);
	}

	public PageResult query(PageResult pageResult, T obj) {
		return list(pageResult, obj);
	}

	public PageResult list(PageResult pageResult, T obj) {
		return getRes(pageResult, dateManage.createCriteria(obj.getClass()));
	}

	protected PageResult getRes(PageResult pageResult, Criteria createCriteria) {
		pageResult.setResLst(createCriteria.list());
		return pageResult;
	}
	protected void throwException(T item) throws NutException{
		throw new NutException(GlobalMessage.REC_ISUSE_ERR);		
	}
}
